package com.jader.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jader.myviews.AlertDialog;
import com.jader.util.JaderApplication;
import com.jader.util.PreferenceUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class WalletActivity extends Activity implements OnClickListener {
	ImageView img_wallet_back;
	TextView tv_wallet_title, tv_wallet_balance;
	Button btn_recharge, btn_getmoney;
	ListView walletListView;
	private HttpUtils httpUtils;
	private String url = JaderApplication.getIP() + "/Jader/client";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wallet);
		initView();
	}

	@Override
	protected void onResume() {
		double balance = JaderApplication.user.getMoney();
		if (balance != 0) {
			tv_wallet_balance.setText(balance + "");
		} else {
			tv_wallet_balance.setText("空空如也");
		}
		super.onResume();
	}

	private void initView() {
		img_wallet_back = (ImageView) findViewById(R.id.top_actionbar_back);
		tv_wallet_title = (TextView) findViewById(R.id.top_actiobar_text);
		tv_wallet_title.setText("我的钱包 ");
		tv_wallet_balance = (TextView) findViewById(R.id.tv_wallet_balance);
		btn_getmoney = (Button) findViewById(R.id.btn_wallet_getmoney);
		btn_recharge = (Button) findViewById(R.id.btn_wallet_recharge);
		img_wallet_back.setOnClickListener(this);
		btn_getmoney.setOnClickListener(this);
		btn_recharge.setOnClickListener(this);
		walletListView = (ListView) findViewById(R.id.listview_wallet);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.top_actionbar_back:
			finish();
			break;
		case R.id.btn_wallet_recharge:
			final AlertDialog dialog = new AlertDialog(WalletActivity.this)
					.builder();
			dialog.setTitle("请输入充值号").setMsg();
			dialog.setPositiveButton("确定", new OnClickListener() {

				@Override
				public void onClick(View v) {
					final double wallet = JaderApplication.user.getMoney() + 999999;
					String money = dialog.getText();
					if (money.equals("147852963")) {
						httpUtils = new HttpUtils();
						RequestParams params = new RequestParams();
						params.addBodyParameter("type", ""
								+ JaderApplication.MY_WALLET);
						params.addBodyParameter("userid", PreferenceUtil
								.getInstance(getBaseContext()).getShareUserId());
						params.addBodyParameter("money", money);
						httpUtils.send(HttpMethod.POST, url, params,
								new RequestCallBack<String>() {

									@Override
									public void onFailure(HttpException arg0,
											String arg1) {
										JaderApplication.show(getBaseContext(),
												"充值失败");
									}

									@Override
									public void onSuccess(
											ResponseInfo<String> arg0) {
										JaderApplication.show(getBaseContext(),
												"充值成功");
										PreferenceUtil.getInstance(
												getBaseContext())
												.setShareUserMoney(wallet);
										JaderApplication.user.setMoney(wallet);
										tv_wallet_balance.setText(wallet + "");
									}
								});
					} else {
						JaderApplication.show(getBaseContext(), "充值卡账号有误");
					}
				}
			}).setNegativeButton("取消", null).show();
			break;
		case R.id.btn_wallet_getmoney:
			JaderApplication.show(this, "暂时不能提现 ");
			break;
		default:
			break;
		}

	}
}
